/**    
 * 文件名：LandseaException.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年6月27日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.common.exception;

import com.logic.landseaserver.common.LandseaErrorMessageEnum;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：LandseaException</br>
 * 类描述：自定义异常，封装了异常码及堆栈</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年6月27日 下午3:39:54</br>
 * @version 1.0
 *
 */
public class LandseaException extends Exception
{
    private static final long serialVersionUID = 4428007830479600164L;
    
    /** 老板错误码 **/
    protected LandseaErrorMessageEnum errorEnum;
    
    /** 新版错误码 **/
    protected ResultCode resultCode;
    
    private boolean isBusinessException;
    
    private LandseaException(LandseaErrorMessageEnum errorEnum, boolean isBusinessException)
    {   
        super();
        this.errorEnum = errorEnum;
        this.isBusinessException = isBusinessException;
    }
    
    private LandseaException(LandseaErrorMessageEnum errorEnum, boolean isBusinessException, Throwable cause)
    {   
        super(cause);
        this.errorEnum = errorEnum;
        this.isBusinessException = isBusinessException;
    }
    
    
    private LandseaException(ResultCode resultCode, boolean isBusinessException)
    {   
        super();
        this.resultCode = resultCode;
        this.isBusinessException = isBusinessException;
    }
    
    private LandseaException(ResultCode resultCode, boolean isBusinessException, Throwable cause)
    {   
        super(cause);
        this.resultCode = resultCode;
        this.isBusinessException = isBusinessException;
    }

    /**
     * 
     * [简要描述]：获取异常自定义描述</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-06-27)</br>
     *
     * @return 错误描述
     *
     */
    public String getErrorMsg()
    {
        if (null != errorEnum)
        {
            return "errorCode=" + errorEnum.getCode() + ",errorMsg=" + errorEnum.getDesc();
        }
        if (null != resultCode)
        {
            return "errorCode=" + resultCode.getCode() + ",errorMsg=" + resultCode.getDesc();
        }
        return "";
    }
    
    public ResultCode getResultCode()
    {
        return resultCode;
    }
    
    /**
     * 
     * [简要描述]：获取异常自定义错误码</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-06-27)</br>
     *
     * @return 错误码
     *
     */
    public String getErrroCode()
    {
        if (null != resultCode)
        {
            return resultCode.getCode();
        }
        return errorEnum.getCode();
    }
    
    public String getErrorDesc()
    {
        if (null != resultCode)
        {
            return resultCode.getDesc();
        }
        return errorEnum.getDesc();
    }
    
    public LandseaErrorMessageEnum getErrorEnum()
    {
        return errorEnum;
    }
    
    public boolean isBusinessException()
    {
        return isBusinessException;
    }
    
    /**
     * 
     * [简要描述]：构造一个无堆栈的异常</br>
     * [详细描述]：一般是业务异常</br>
     * [作者]：Aaron(2017-06-27)</br>
     *
     * @param errorEnum 错误对象
     * @return LandseaException
     *
     */
    public static LandseaException createException(LandseaErrorMessageEnum errorEnum)
    {
        LandseaException exception = new LandseaException(errorEnum,true);
        return exception;
    }
    
    public static LandseaException createException(ResultCode resultCode)
    {
        LandseaException exception = new LandseaException(resultCode,true);
        return exception;
    }
    
    /**
     * 
     * [简要描述]：构造一个带有堆栈的异常</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-06-27)</br>
     *
     * @param errorEnum 错误对象
     * @param cause 堆栈
     * @return LandseaException
     *
     */
    public static LandseaException createException(LandseaErrorMessageEnum errorEnum, Throwable cause)
    {
        LandseaException exception = new LandseaException(errorEnum, false, cause);
        return exception;
    }
    
    public static LandseaException createException(ResultCode resultCode, Throwable cause)
    {
        LandseaException exception = new LandseaException(resultCode, false, cause);
        return exception;
    }
}
